{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "atlantic-merit",
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "import os\n",
    "import pandas as pd\n",
    "from py2neo import Node, Graph, Relationship, NodeMatcher\n",
    "\n",
    "# 配置类\n",
    "class CONFIG(enumerate):\n",
    "  # 这是我自己的Neo4j服务器访问地址\n",
    "  # 可以使用实验室服务器的jupyter连接\n",
    "  URL = 'http://172.17.0.1:1074' # 服务器Neo4j地址：http://server.322lab.kust.cn:1074\n",
    "  # 用户名\n",
    "  USERNAME = 'neo4j'             # 服务器Neo4j用户名：neo4j\n",
    "  # 密码\n",
    "  PASSWORD = 'ljx62149079'       # 服务器Neo4j密码：322lab\n",
    "  # csv文件路径\n",
    "  CSV = os.getcwd() + '/task_architect.csv'\n",
    "  pass\n",
    "\n",
    "class DataImporter:\n",
    "  def __init__(self):\n",
    "    # 读取所有数据\n",
    "    self.raw_data = self.get_csv_data(CONFIG.CSV)\n",
    "    # 主体\n",
    "    self.subjects = list(set(self.raw_data['subject']))\n",
    "    # 课题\n",
    "    self.relations = list(set(self.raw_data['relation']))\n",
    "    # 客体\n",
    "    self.objects = list(set(self.raw_data['object']))\n",
    "    # 创建图\n",
    "    self.graph = Graph(\n",
    "      CONFIG.URL,\n",
    "      username=CONFIG.USERNAME,\n",
    "      password=CONFIG.PASSWORD\n",
    "    )\n",
    "    # 清除以前的缓存\n",
    "    self.graph.delete_all()\n",
    "    # 匹配器\n",
    "    self.matcher = NodeMatcher(self.graph)\n",
    "    # 字典\n",
    "    self.dictionary = pd.DataFrame({\n",
    "      'subject': list(self.raw_data['subject']),\n",
    "      'object': list(self.raw_data['object']),\n",
    "      'relation': list(self.raw_data['relation'])\n",
    "    })\n",
    "    pass\n",
    "\n",
    "  def get_csv_data(self, path):\n",
    "    return pd.read_csv(path)\n",
    "    pass\n",
    "  # 创建节点\n",
    "  def create_tree_node(self, subjects, objects):\n",
    "    for key in set(dataImporter.raw_data['subject']).union(set(dataImporter.raw_data['object'])):\n",
    "      self.graph.create(Node('subject', name=key))\n",
    "      pass\n",
    "    pass\n",
    "  # 创建关系\n",
    "  def create_nodes_relation(self):\n",
    "    for item in range(0, len(self.dictionary)):\n",
    "      try:\n",
    "        self.graph.create(\n",
    "          Relationship(\n",
    "            self.matcher.match('subject').where(\"_.name='\" + self.dictionary['subject'][item] + \"'\").first(),\n",
    "            self.dictionary['relation'][item],\n",
    "            self.matcher.match('subject').where(\"_.name='\" + self.dictionary['object'][item] + \"'\").first()\n",
    "          )\n",
    "        )\n",
    "        pass\n",
    "      except AttributeError as ae:\n",
    "        print(item, ae)\n",
    "        pass\n",
    "      pass\n",
    "    pass\n",
    "  pass\n",
    "\n",
    "if __name__ == '__main__':\n",
    "  dataImporter = DataImporter()\n",
    "  dataImporter.create_tree_node(dataImporter.subjects, dataImporter.objects)\n",
    "  dataImporter.create_nodes_relation()\n",
    "  pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
